package com.filldream.fastboot.web.controller.pc;


import cn.hutool.core.lang.tree.Tree;
import com.filldream.fastboot.base.entity.Menu;
import com.filldream.fastboot.base.request.IdSTO;
import com.filldream.fastboot.base.request.pc.menu.AddMenuSTO;
import com.filldream.fastboot.base.request.pc.menu.ModifyMenuSTO;
import com.filldream.fastboot.base.request.pc.menu.MoveMenuSTO;
import com.filldream.fastboot.base.response.pc.MenusDTO;
import com.filldream.fastboot.common.annotation.Log;
import com.filldream.fastboot.common.annotation.RolePermissions;
import com.filldream.fastboot.common.entity.B;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.TransferUtil;
import com.filldream.fastboot.core.manager.MenuManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "PC-菜单管理")
@RestController
@RequestMapping("/pc/menu")
public class PcMenuController {

    @Resource
    private MenuManager menuManager;

    @Log(value = "查询全部菜单及按钮")
    @ApiOperation(value = "查询全部菜单及按钮")
    @GetMapping("/getAllMenu")
    public R<List<Tree<String>>> getAllMenu2(){
        return R.autoSuccess(menuManager.getMenuList(null));
    }

    @Log(value = "查询所有菜单")
    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/getMenuList")
    public R<List<Tree<String>>> getMenuList(){
        return R.autoSuccess(menuManager.getMenuList(1));
    }

    @Log(value = "添加菜单")
    @ApiOperation(value = "添加菜单")
    @PostMapping("/add")
    @RolePermissions("menu:add")
    public R<Menu> add(@RequestBody @Valid AddMenuSTO addMenuSTO){
        addMenuSTO.check();
        return R.autoSuccess(
                menuManager.addMenu(TransferUtil.changeToObj(addMenuSTO,Menu.class))
        );
    }

    @Log(value = "移动菜单")
    @ApiOperation(value = "移动菜单")
    @PostMapping("/move")
    @RolePermissions("menu:move")
    public B move(@RequestBody @Valid MoveMenuSTO moveMenuSTO){
        return menuManager.moveMenu(moveMenuSTO);
    }

    @Log(value = "修改菜单")
    @ApiOperation(value = "修改菜单")
    @PostMapping("/modify")
    @RolePermissions("menu:modify")
    public R<Menu> modify(@RequestBody @Valid ModifyMenuSTO modifyMenuSTO){
        Menu menu = menuManager.updateMenu(TransferUtil.changeToObj(modifyMenuSTO, Menu.class));
        return R.autoSuccess(menu);
    }

    @Log(value = "删除菜单")
    @ApiOperation(value = "删除菜单",notes = "传入菜单ID")
    @PostMapping("/del")
    @RolePermissions("menu:del")
    public B del(@RequestBody IdSTO idSTO){
        return B.checkSuccess( menuManager.delMenu(idSTO.getId()));
    }

    @Log(value = "菜单详情")
    @ApiOperation(value = "菜单详情")
    @GetMapping("/simpleGet")
    public R<Menu> simpleGet(IdSTO idSTO){
        return R.autoSuccess(menuManager.getById(idSTO.getId()));
    }

    @Log(value = "菜单列表")
    @ApiOperation(value = "菜单列表")
    @GetMapping("/list")
    public R<List<Menu>> getAllMenu(IdSTO idSTO){
        return R.autoSuccess( menuManager.getDirectMenuList(idSTO.getId() == null ? 0L : idSTO.getId()));
    }

}
